package DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import Model.DiasSessao;
import Utils.Conexao;
import com.mysql.jdbc.CallableStatement;

public class DiasSessaoDAO {
	public static void Insert(DiasSessao objDiasSessao) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_insert_dias_sessao(?)";		
		CallableStatement call = DiasSessaoDAO.setObjectCallableStatement(objDiasSessao, mysqlCall, con);
		call.execute();
		con.close();		
		call.close();
	}
	
	public static void Update(DiasSessao objDiasSessao) throws SQLException
	{		
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_update_dias_sessao(?, ?)";		
		CallableStatement call = DiasSessaoDAO.setObjectCallableStatement(objDiasSessao, mysqlCall, con) ;
		call.executeUpdate();
		con.close();
		call.close();
	}
	
	
	public static void Delete(int id) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_deletar_dias_sessao(?)";
		CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
		call.setInt(1, id);		
		call.execute();		
		
		con.close();
		call.close();
	}
	
	public static DiasSessao LoadObject(int id_dias) throws SQLException
	{
		DiasSessao dias = null;
		Connection con = Conexao.getConection();
		
		String mysqlCall = "call spc_select_dias_sessao_por_id(?)";
		CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
		call.setInt(1, id_dias);
		ResultSet res = call.executeQuery();		
		
		if(res.next())
			dias = DiasSessaoDAO.setInstance(res);		
		
		call.close();
		con.close();
		return dias;	
	}
	
	public static ArrayList<DiasSessao> GetDiasSessaoArrayList()
	{
		ArrayList<DiasSessao> arrDias = new ArrayList<DiasSessao>(); 
		Connection con  = Conexao.getConection();
		
		try
		{
			String mysqlCall = "call spc_select_dias_sessao()";			
			CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
			ResultSet rs = call.executeQuery();			
			while(rs.next())
			{
				DiasSessao dias = DiasSessaoDAO.setInstance(rs);
				arrDias.add(dias);
			}
			
			call.close();
			con.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return arrDias;
	}
	
	private static CallableStatement setObjectCallableStatement(DiasSessao objDiasSessao, String mysqlCall, Connection con ) throws SQLException
	{	
		CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
		call.setString(1, objDiasSessao.getDescricao());
		if(objDiasSessao.getIdDiasSessao() > 0)
			call.setInt(2, objDiasSessao.getIdDiasSessao());		
		return call; 		
	}
	
	public static DiasSessao setInstance(ResultSet res) throws SQLException
	{
		DiasSessao dias  = new DiasSessao();
		dias.setIdDiasSessao(res.getInt("id_dias_sessao"));
		dias.setDescricao(res.getString("descricao"));				
		return dias;		
	}
	
}
